//
//  PayShowView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class PayShowView: UIView, AddCornerUIRectCorner, BottomShadow {
    
    var mainView = BaseCircleView()
    let allWidth: CGFloat = 242
    let payTypeViewHeight: CGFloat = 41
    private let topOffset: CGFloat = 40
    private let priceTopOffset: CGFloat = 15
    private let orderTopOffset: CGFloat = 22
    private let lineTopOffset: CGFloat = 5
    private let bottomOffset: CGFloat = 32
    private let doneButtonHeight: CGFloat = 40
    private let bottomViewHeight: CGFloat = kHeight(r: 43)
    var payClosure:(() -> Void)?
    // 以后这里加一个参数传进来
    func showData(payClousure: @escaping () -> Void) {
        self.payClosure = payClousure
        let window = UIApplication.shared.keyWindow
        self.frame = window!.bounds
        self.alpha = 0.5
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1
        }
        mainView.backgroundColor = UIColor.white
        mainView.width = allWidth
        self.addSubview(mainView)
        
        let payTypeView = UIView()
        payTypeView.frame = CGRect(x: 0, y: 0, width: allWidth, height: payTypeViewHeight)
        mainView.addSubview(payTypeView)
        
        let button = UIButton(type: .custom)
        button.isUserInteractionEnabled = false
        button.setTitle("支付宝", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setImage(UIImage(named: "aliPay_iocn"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.sizeToFit()
        button.width += 20
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
        button.center = CGPoint(x: payTypeView.width / 2, y: payTypeView.height / 2)
        payTypeView.addSubview(button)
        
        let lineView = UIView()
        lineView.backgroundColor = ONColor.cutLineColor
        lineView.frame = CGRect(x: 0, y: payTypeView.bottom, width: payTypeView.width, height: 1)
        mainView.addSubview(lineView)
        
        let priceLabel = UILabel()
        priceLabel.textColor = UIColor(hex: 0x176DC7)
        priceLabel.text = "￥5899.00"
        priceLabel.sizeToFit()
        priceLabel.frame.origin = CGPoint(x: mainView.width / 2 - priceLabel.width / 2, y: lineView.bottom + priceTopOffset)
        mainView.addSubview(priceLabel)
        
        let orderNumberLabel = UILabel()
        orderNumberLabel.font = UIFont.systemFont(ofSize: 12)
        orderNumberLabel.text = "   支付宝 【一度蜜支付订单】23456677711"
        orderNumberLabel.frame = CGRect(x: 0, y: priceLabel.bottom + orderTopOffset, width: allWidth, height: 14)
        mainView.addSubview(orderNumberLabel)
        
        let orderNumberlineView = UIView()
        orderNumberlineView.backgroundColor = ONColor.cutLineColor
        orderNumberlineView.frame = CGRect(x: 0, y: orderNumberLabel.bottom + lineTopOffset, width: payTypeView.width, height: 1)
        mainView.addSubview(orderNumberlineView)
        
        let payTypeLabel = UILabel()
        payTypeLabel.font = UIFont.systemFont(ofSize: 12)
        payTypeLabel.text = "  付款方式             \("账户余额")"
        payTypeLabel.frame = CGRect(x: 0, y: orderNumberlineView.bottom + orderTopOffset, width: allWidth, height: 14)
        mainView.addSubview(payTypeLabel)
        
        let payTypelineView = UIView()
        payTypelineView.backgroundColor = ONColor.cutLineColor
        payTypelineView.frame = CGRect(x: 0, y: payTypeLabel.bottom + lineTopOffset, width: payTypeView.width, height: 1)
        mainView.addSubview(payTypelineView)
        
        let doneButton = UIButton(type: .custom)
        doneButton.setTitle("立即支付", for: .normal)
        doneButton.backgroundColor = UIColor(hex: 0x01A0EB)
        doneButton.addTarget(self, action: #selector(doneTap), for: .touchUpInside)
        doneButton.frame = CGRect(x: 0, y: payTypelineView.bottom + bottomOffset, width: allWidth, height: doneButtonHeight)
        doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        mainView.addSubview(doneButton)
        mainView.height = doneButton.bottom
        mainView.center = CGPoint(x: self.width / 2, y: self.height / 2 - topOffset)
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        
        addCornerWithView(aView: payTypeView, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: doneButton, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
    }
    
    @objc func doneTap() {
        payClosure?()
        hide()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let view = touches.first?.view {
            if view == self {
                hide()
            }
        }
    }
    
    // MARK: - 消失
    private func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.2
        }) { (make) in
            self.removeFromSuperview()
        }
    }
    
}
